//
//  HTTPMethod.m
//  NSURLSession版网络请求(AFN3.0)
//
//  Created by dllo on 15/10/27.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "HTTPMethod.h"


@implementation HTTPMethod
+ (void)getDataByString:(NSString *)urlString BodyDic:(NSDictionary *)bodyDic WithDataBlock:(void (^)(id))dataBLock
{
    //1. 字符串的转码
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
    //2.创建管理者对象(sesson)
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //3.设置允许请求的类别
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js",@"application/x-javascript", nil];
    
    
    
    
    //4.开始请求
    if (!bodyDic) {
        //如果bodyDic为空就执行Get请求
        [manager GET:urlString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            dataBLock(responseObject);
            
            //缓存
            //创建浏览历史的路径
            NSFileManager *manager = [NSFileManager defaultManager];
            //获取完整路径
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            NSString *cachesPath = [path stringByAppendingPathComponent:@"Caches/FreeTimeCaches"];
            [manager createDirectoryAtPath:cachesPath withIntermediateDirectories:YES attributes:nil error:nil];
            
           NSString *subPath = [cachesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld.plist", [urlString hash]]];

            [responseObject writeToFile:subPath atomically:YES];
            NSLog(@"%@", subPath);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败");
           // NSString *str = [NSString stringWithFormat:@"%ld.plist", [urlString hash]];
            //获取完整路径
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            NSString *cachesPath = [path stringByAppendingPathComponent:@"Caches/FreeTimeCaches/"];
            NSString *subCachesPath = [cachesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld.plist", [urlString hash]]];
             NSDictionary *responseObject = [NSDictionary dictionaryWithContentsOfFile:subCachesPath];
            dataBLock(responseObject);
        }];
    }
    else{
        [manager POST:urlString parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            dataBLock(responseObject);
            
            //缓存
            //创建浏览历史的路径
            NSFileManager *manager = [NSFileManager defaultManager];
            //获取完整路径
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            NSString *cachesPath = [path stringByAppendingPathComponent:@"Caches/FreeTimeCaches"];
            [manager createDirectoryAtPath:cachesPath withIntermediateDirectories:YES attributes:nil error:nil];
            
            NSString *subPath = [cachesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld.plist", [urlString hash]]];
            
            [responseObject writeToFile:subPath atomically:YES];
            NSLog(@"%@", subPath);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败");
            // NSString *str = [NSString stringWithFormat:@"%ld.plist", [urlString hash]];
            //获取完整路径
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            NSString *cachesPath = [path stringByAppendingPathComponent:@"Caches/FreeTimeCaches/"];
            NSString *subCachesPath = [cachesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld.plist", [urlString hash]]];
            NSDictionary *responseObject = [NSDictionary dictionaryWithContentsOfFile:subCachesPath];
            dataBLock(responseObject);
        }];
    }
}
@end
